# usage: query.py db_file ontology output_file query
# На вход должны приходить уже нормальные формы

import sqlite3, sys, os, re

def ConnectToDB(dbName):
    if (not os.path.exists(dbName)):
        print("Не найден файл базы данных, проверьте его доступность")
        sys.exit(1)
    
    db = sqlite3.connect(dbName)
    if (db == None):
        print("Ошибка подключения к базе данных")
        sys.exit(1)
    
    return db

class Ontology:
    entities = {}
    rules = []
    collocations = []
    extralingInfo = []
    regexes = {}
    queries = []
    
    def LoadDBData (self, dbname):
        for entity in entities:
            if (
    
    def ApplyRules (self, text):
        for rule in rules:
            text = text.replace (rule, rules[rule])
            return text
    
    def ParseEntity(self, line):
        return line
    
    def ParseRule(self, line):
        rule = line.split(" -> ")
        return rule
    
    def ParseCollocation(self, line):
        match = re.search("([^)]*)", line)
        if (match != None):
            return line.replace(match.group(0), self.ParseEntity(match.group(1)))
        return line
    
    def ParseExtraling(self, line):
        extralingEquiv = line.split(" = ")
        return extralingEquiv
    
    def ParseQuery(self, line):
        return line.split(" ")
    
    def LoadOntology(self):
        with open(sys.argv[2]) as ontologyFile:
            curState = "none"
            for line in ontologyFile:
                line = line.strip("\n")
                if (line == "" or line[0] == ";"):
                    continue
                
                if (line[0] == "["):
                    curState = line[1:-1]
                    continue
                
                if (curState == "rules"):
                    self.rules.append(ParseRule(line))
                elif (curState == "entities"):
                    self.entities[1:line.find(")")] = ParseEntity(line[line.find("= ") + 2:])
                elif(curState == "collocations"):
                    self.collocations.append(ParseCollocation("line"))
                elif (curState == "extralingInfo"):
                    self.extralingInfo.append(ParseExtraling(line))
                elif (curState == "regexes"):
                    self.regexes[line.split(" = ")[0]] = line.split(" = ")[1]
                elif (curState == "queries"):
                    self.queries.append(ParseQuery(line))
                    
    def __init__(self):
        LoadOntology()

sys.argv = ("\"" + "\" \"".join(sys.argv) + "\"")[1:-1].split("\" \"")
if (len(sys.argv) < 5):
    print ("использование: query.py db_file ontology_file output_file query")
    sys.exit(1)

ontology = LoadOntology(sys.argv[2])
ontology.LoadDBData(sys.argv[1])
query = ontology.ApplyRules(sys.argv[4])

tokens = GetTokens(sys.argv[4])
for token in tokens:
    if (token in ontology):
        